<template>
	<view class="contact-us-container">
		<text class="contact-us-title">联系我们</text>

		<view class="contact-info">
			<text class="info-title">位置地图</text>
			<view class="map-container" @tap="openFullMap">
				<view class="map-content">
					<text class="school-address">浙江省宁波市江北区风华路495号</text>
				</view>
				<view class="map-tip">点击查看详细地图</view>
			</view>
		</view>

		<view class="contact-item">
			<text class="label">在线客服：</text>
			<button class="chat-button" @tap="openChat">
				<text>💬</text>
				<text class="button-text">点击咨询</text>
			</button>
		</view>

		<!-- 原生悬浮聊天窗口（替代uni-popup） -->
		<view v-if="chatOpen" class="chat-window" @touchmove.stop>
			<view class="chat-header">
				<text class="header-title">在线客服</text>
				<button class="close-btn" @tap="closeChat">×</button>
			</view>
			<scroll-view class="chat-messages" scroll-y :scroll-top="scrollTop" scroll-with-animation>
				<view v-for="(message, index) in chatMessages" :key="index" class="message-item">
					<view v-if="message.type === 'system'" class="system-message">
						<text>{{ message.content }}</text>
					</view>
					<view v-if="message.type === 'user'" class="user-message">
						<text>{{ message.content }}</text>
					</view>
				</view>
			</scroll-view>
			<view class="chat-input">
				<input type="text" v-model="message" placeholder="输入您的问题..." @confirm="sendMessage" class="input-field">
				<button class="send-btn" @tap="sendMessage" :disabled="!message.trim()">发送</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'ContactUs',
		data() {
			return {
				chatOpen: false,
				message: '',
				scrollTop: 0,
				// 浙江纺织服装职业技术学校坐标
				mapCenter: {
					lng: 121.5715,
					lat: 29.9147
				},
				chatMessages: [{
					type: 'system',
					content: '客服已上线，请问有什么可以帮助您？'
				}]
			};
		},
		methods: {
			openChat() {
				this.chatOpen = true;
				// 打开后自动聚焦输入框（Uniapp原生兼容）
				this.$nextTick(() => {
					uni.createSelectorQuery().select('.input-field').fields({
						node: true
					}).exec(res => {
						if (res[0]?.node) res[0].node.focus();
					});
				});
			},
			closeChat() {
				this.chatOpen = false;
			},
			sendMessage() {
				if (this.message.trim() === '') return;

				// 添加用户消息
				this.chatMessages.push({
					type: 'user',
					content: this.message
				});

				// 滚动到底部
				this.$nextTick(() => {
					// 原生scroll-view滚动到底部
					const query = uni.createSelectorQuery().in(this);
					query.select('.chat-messages').boundingClientRect(rect => {
						this.scrollTop = rect.height;
					}).exec();
				});

				// 模拟客服回复
				setTimeout(() => {
					this.chatMessages.push({
						type: 'system',
						content: '感谢您的咨询，我们会尽快处理您的问题。'
					});
					this.$nextTick(() => {
						const query = uni.createSelectorQuery().in(this);
						query.select('.chat-messages').boundingClientRect(rect => {
							this.scrollTop = rect.height;
						}).exec();
					});
				}, 500);

				this.message = '';
			},
			openFullMap() {
				// 原生地图API（无需依赖）
				uni.getLocation({
					type: 'gcj02',
					success: () => {
						uni.openLocation({
							longitude: this.mapCenter.lng,
							latitude: this.mapCenter.lat,
							name: '浙江纺织服装职业技术学院',
							address: '浙江省宁波市江北区风华路495号',
							scale: 18,
							fail: () => {
								uni.showToast({
									title: '打开地图失败',
									icon: 'none'
								});
							}
						});
					},
					fail: () => {
						uni.showToast({
							title: '请先开启位置权限',
							icon: 'none'
						});
					}
				});
			}
		}
	};
</script>

<style scoped>
	.contact-us-container {
		max-width: 800px;
		margin: 0 auto;
		padding: 20rpx;
		border: 1rpx solid #e0e0e0;
		border-radius: 10rpx;
		box-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.05);
		background-color: #fff;
		font-family: Arial, sans-serif;
		line-height: 1.6;
	}

	.contact-us-title {
		font-size: 32rpx;
		color: #333;
		margin-bottom: 20rpx;
		text-align: center;
		display: block;
		font-weight: bold;
		text-shadow: 1rpx 1rpx 2rpx rgba(0, 0, 0, 0.1);
	}

	.contact-info {
		margin-bottom: 20rpx;
		border: 1rpx solid #e0e0e0;
		border-radius: 10rpx;
		padding: 15rpx;
		background-color: #f9f9f9;
	}

	.info-title {
		font-size: 24rpx;
		color: #333;
		margin-bottom: 10rpx;
		border-bottom: 2rpx solid #007bff;
		padding-bottom: 5rpx;
		display: block;
		font-weight: bold;
	}

	.contact-item {
		display: flex;
		align-items: center;
		margin-bottom: 20rpx;
	}

	.label {
		font-weight: bold;
		margin-right: 10rpx;
		font-size: 16rpx;
		color: #333;
	}

	.map-container {
		width: 100%;
		height: 300rpx;
		margin-top: 10rpx;
		position: relative;
		border: 1rpx solid #e0e0e0;
		background-color: #f5f5f5;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.map-content {
		color: #333;
		text-align: center;
	}

	.school-address {
		font-size: 18rpx;
		font-weight: bold;
		margin-bottom: 5rpx;
		display: block;
	}

	.map-tip {
		position: absolute;
		bottom: 10rpx;
		left: 10rpx;
		background-color: rgba(0, 0, 0, 0.5);
		color: white;
		padding: 5rpx 10rpx;
		border-radius: 6rpx;
		font-size: 14rpx;
		z-index: 1;
	}

	.chat-button {
		background-color: #007bff;
		color: white;
		border: none;
		padding: 8rpx 15rpx;
		border-radius: 8rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 16rpx;
	}

	.chat-button text:first-child {
		margin-right: 5rpx;
	}

	/* 原生悬浮聊天窗口（替代uni-popup） */
	.chat-window {
		position: fixed;
		bottom: 20rpx;
		right: 20rpx;
		width: 350px;
		max-width: 90%;
		height: 600rpx;
		background-color: white;
		border-radius: 10rpx;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
		z-index: 9999;
		display: flex;
		flex-direction: column;
	}

	.chat-header {
		background-color: #007bff;
		color: white;
		padding: 10rpx 15rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-top-left-radius: 10rpx;
		border-top-right-radius: 10rpx;
	}

	.header-title {
		font-size: 18rpx;
		font-weight: bold;
	}

	.close-btn {
		background: none;
		border: none;
		color: white;
		font-size: 24rpx;
		padding: 0;
	}

	.chat-messages {
		flex: 1;
		padding: 15rpx;
		background-color: #f5f5f5;
	}

	.message-item {
		margin-bottom: 15rpx;
		display: flex;
	}

	.system-message {
		background-color: #e6f7ff;
		padding: 8rpx 12rpx;
		border-radius: 8rpx;
		max-width: 80%;
		align-self: flex-start;
	}

	.user-message {
		background-color: #dcf8c6;
		padding: 8rpx 12rpx;
		border-radius: 8rpx;
		max-width: 80%;
		align-self: flex-end;
		margin-left: auto;
	}

	.chat-input {
		padding: 10rpx 15rpx;
		display: flex;
		border-top: 1rpx solid #e0e0e0;
	}

	.input-field {
		flex: 1;
		padding: 10rpx;
		border: 1rpx solid #e0e0e0;
		border-radius: 8rpx 0 0 8rpx;
		outline: none;
		font-size: 16rpx;
	}

	.send-btn {
		background-color: #007bff;
		color: white;
		border: none;
		padding: 10rpx 15rpx;
		border-radius: 0 8rpx 8rpx 0;
		font-size: 16rpx;
	}

	.send-btn:disabled {
		background-color: #ccc;
		cursor: not-allowed;
	}

	/* 适配小屏幕 */
	@media (max-width: 375px) {
		.chat-window {
			width: calc(100% - 40rpx);
			height: 500rpx;
		}
	}
</style>